const mapUtils = require('../../libs/mapsdk.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: 0,
    gasStations: [
      
    ],
    markers: [],
    longitude: 116.397390,
    latitude: 39.908860,
    scale: 14
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取状态栏高度
    const systemInfo = wx.getSystemInfoSync();
    this.setData({
      statusBarHeight: systemInfo.statusBarHeight
    });
    
    mapUtils.searchNearby('加油站',res =>{
      console.log("加油站",res);
      this.setData({
        gasStations:res
      })
    });
  },

  // 获取附近加油站(模拟数据)
  getNearbyGasStations(latitude, longitude) {
    // 真实项目中应该通过API获取附近加油站数据
    // 这里使用静态数据模拟
    const markers = this.data.gasStations.map(station => ({
      id: station.id,
      latitude: station.latitude,
      longitude: station.longitude,
      width: 30,
      height: 30,
      iconPath: '/assets/images/gas-station-marker.png',
      callout: {
        content: station.name,
        color: '#000000',
        fontSize: 12,
        borderRadius: 4,
        padding: 5,
        display: 'BYCLICK'
      }
    }));

    this.setData({ markers });
  },

  // 点击加油站
  onGasStationTap(e) {
    const stationId = e.currentTarget.dataset.id;
    const station = this.data.gasStations.find(item => item.id === stationId);
    
    // 在地图上选中该加油站
    this.setData({
      latitude: station.latitude,
      longitude: station.longitude,
      scale: 16
    });
  },

  // 导航到加油站
  navigateToGasStation(e) {
    const stationId = e.currentTarget.dataset.id;
    const station = this.data.gasStations.find(item => item.id === stationId);
    
    wx.openLocation({
      latitude: station.latitude,
      longitude: station.longitude,
      name: station.name,
      address: station.address,
      scale: 18
    });
  },

  // 返回上一页
  handleBack() {
    wx.navigateBack();
  }
}) 